// pages/register/driverCar/driverCar.js
import http from '../../../../services/http/index'
let util = require('../../../../utils/index')
import Toast from "../../../components/toast/toast.js";
import config from '../../../../config/index'
import { ListData } from '../../../../utils/Data'
const { api: { url: apiBaseUrl } } = config

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ctype:0,
    chide:true,
    confirmBtn: {
      disabled: true,
    },

    infoListArr:[],//前一个页面传过来的数据

    car_type:'',//车型
    car_no: '',//车牌号
    id_card: '',//护照号
    car_brand: '',//汽车品牌
    person_limit: '',//乘坐乘客上限
    baggage_limit: '',//行李承载量上限
    is_supply_children_seat: '',//是否提供儿童座椅
    
    insurance_img_id: '', // 全险照片图片id
    insurance_img_path: "", //全险照片图片路径

    img_id: '', //手持护照照片id
    img_path: "",//手持护照图片路径

    driver_img_id: '',//驾照照片id
    driver_img_path: "",//驾照照片图片路径

    car_img_front_id: '',//汽车正面照照片id
    car_img_front_path: "",//汽车正面照照片路径

    car_img_side_id: '',//汽车侧面照照片id
    car_img_side_path: "",//汽车侧面照图片路径

    car_interior_id:'',//内饰照片
    car_interior_path:"",


    typePath:'',

    mobile: '',//手机号
    checks1: '',   //判断是否提供儿童座椅
  },
  // 选中-是否提供儿童座椅
  checkboxChange1: function (e) {
    console.log(e.target.dataset)
    console.log(e.target.dataset.checks)
    if (e.target.dataset.checks == 0 || e.target.dataset.checks == '') {
      this.setData({
        checks1: 1
      })
    } else {
      this.setData({
        checks1: 0
      })
    }
  },

  uploadImg(e) {
    var _this = this;
    var type =e.currentTarget.dataset.type;
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        console.log(res);
        wx.showToast({
          title: 'loading',
          icon: 'loading',
          duration: 10000
        });
        var tempFilePaths = res.tempFilePaths[0];
        wx.uploadFile({
          url: apiBaseUrl + '/system/upload',      //此处换上你的接口地址
          filePath: tempFilePaths,
          name: 'file',
          header: {
            "Content-Type": "multipart/form-data",
            'accept': 'application/json',
            'token': 'wqeqewqeq'
          },
          formData: {},
          success: function (res) {
            wx.hideToast();
            var data = JSON.parse(res.data);
            console.log(data);            
            if (res.statusCode == 200) {
              console.log(data.data.link)
              console.log(type + '_path');
              if (type =='insurance_img'){
                _this.setData({
                  insurance_img_path:data.data.link,
                  insurance_img_id: data.data.id
                })
              } else if (type == 'img'){
                _this.setData({
                  img_path: data.data.link,
                  img_id: data.data.id
                })
              } else if (type == 'driver_img') {
                _this.setData({
                  driver_img_path: data.data.link,
                  driver_img_id: data.data.id
                })
              } else if (type == 'car_img_front') {
                _this.setData({
                  car_img_front_path: data.data.link,
                  car_img_front_id: data.data.id
                })
              } else if (type == 'car_img_side') {
                _this.setData({
                  car_img_side_path: data.data.link,
                  car_img_side_id: data.data.id
                })
              }else if(type == 'car_interior'){
                _this.setData({
                  car_interior_path: data.data.link,
                  car_interior_id: data.data.id
                })
              }
              // 判断是否整个页面信息为空
              _this.checkKong();
              // _this.data.type + 'path' = data.data.link;
              // type
              //图片地址:data.data.link  图片id:data.data.id
              //这里是上传成功后的图片地址和id,你直接替换给你的图片数据

            } else {
              Toast.show(_this, 'info', '上传服务器失败');
            }

          },
          fail: function (res) {
            wx.hideToast();
            console.log('fail');
          },
        })

      },
      fail: function () {
        Toast.show(_this, 'info', '选择图片失败');
      }
    });
  },
  // 输入文本框时判断全局数据是否为空
  changeCheck1(e){
    this.setData({
      car_type: e.detail.value
    })
    this.checkKong();
  },
  changeCheck2(e){
     this.setData({
       car_no: e.detail.value
     })
    this.checkKong();
  },
  changeCheck3(e) {
    this.setData({
      id_card: e.detail.value
    })
    this.checkKong();
  },
  changeCheck4(e) {
    this.setData({
      car_brand: e.detail.value
    })
    this.checkKong();
  },
  changeCheck5(e) {
    this.setData({
      person_limit: e.detail.value
    })
    this.checkKong();
  },
  changeCheck6(e) {
    this.setData({
      baggage_limit: e.detail.value
    })
    this.checkKong();
  },

  next(){
    util.default.go('../../../commitSuccess/commitSuccess')
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    // 页面初始化获取数据
    var moblie=options.mobile;
    var infoListArr = JSON.parse(options.infoList);
    that.setData({
      infoListArr: infoListArr,

      car_type: infoListArr.car_type,
      car_no: infoListArr.car_no,
      id_card: infoListArr.id_card,
      car_brand: infoListArr.car_brand,
      person_limit: infoListArr.person_limit,
      baggage_limit: infoListArr.baggage_limit,
      is_supply_children_seat: infoListArr.is_supply_children_seat,
      insurance_img_id: infoListArr.insurance_img_id,
      insurance_img_path: infoListArr.insurance_img_path,
      img_id: infoListArr.img_id, 
      img_path: infoListArr.img_path,
      driver_img_id: infoListArr.driver_img_id,
      driver_img_path: infoListArr.driver_img_path,
      car_img_front_id: infoListArr.car_img_front_id,
      car_img_front_path: infoListArr.car_img_front_path,
      car_img_side_id: infoListArr.car_img_side_id,
      car_img_side_path: infoListArr.car_img_side_path,
      car_interior_id:infoListArr.car_interior_id,
      car_interior_path:infoListArr.car_interior_path,
      mobile: moblie
    })
    // 座椅选中状态
    if (infoListArr.is_supply_children_seat==0){
      that.setData({
        checks1:0
      })
    }else{
      that.setData({
        checks1: 1
      })
    }
    that.checkKong();

  },

  // 判断整个内容是否为空
  checkKong(){
    var that=this;
    var car_type = that.data.car_type;
    var car_no = that.data.car_no;
    var id_card = that.data.id_card;
    var car_brand = that.data.car_brand;
    var person_limit = that.data.person_limit;
    var baggage_limit = that.data.baggage_limit;
    var insurance_img_id = that.data.insurance_img_id;
    var img_id = that.data.img_id;
    var driver_img_id = that.data.driver_img_id;
    var car_img_front_id = that.data.car_img_front_id;
    var car_img_side_id = that.data.car_img_side_id;
    var car_interior_id = that.data.car_interior_id;

    if (car_type == '' || car_no == '' || id_card == '' || car_brand == '' || person_limit == '' || baggage_limit == '' || insurance_img_id == 0 || img_id == 0 || driver_img_id == 0 || car_img_front_id == 0 || car_img_side_id == 0 || car_interior_id == 0){
      // wx.showToast({
      //   title: `请输入完整信息`,
      //   icon: 'none'
      // });
      
      that.setData({
        'confirmBtn.disabled': true
      })
    } else {
      that.setData({
        'confirmBtn.disabled': false
      })
    }

  },
  // 提交申请
  formSubmit: function (e) {
    var that = this;
    console.log('form发生了submit事件，携带数据为：')
    console.log(e.detail.value)
    console.log(e.detail.value.is_supply_children_seat)
    // that.pandunKong_zui();
    if (e.detail.value.baggage_limit == '' || e.detail.value.car_brand == '' ||e.detail.value.car_img_front_id==0 ||
      e.detail.value.car_img_side_id == 0 || e.detail.value.car_interior_id == 0 || e.detail.value.car_no == '' || e.detail.value.car_type == '' ||
      e.detail.value.driver_img_id == 0 || e.detail.value.id_card == '' || e.detail.value.img_id == 0 ||
      e.detail.value.insurance_img_id == 0 || e.detail.value.person_limit == '') {
      wx.showToast({
        title: `请输入完整信息`,
        icon: 'none'
      });
    } else {
      // 提交数据---提交注册信息（第三步）
      var param = ({
       // baggage_limit: e.detail.value.baggage_limit,//承载量上限
        car_brand: e.detail.value.car_brand,//汽车品牌
        car_img_front_id: e.detail.value.car_img_front_id,
        car_img_side_id: e.detail.value.car_img_side_id,
        car_interior_id:e.detail.value.car_interior_id,//内饰照片
        car_no: e.detail.value.car_no,
        car_type: e.detail.value.car_type,//车型
        driver_img_id: e.detail.value.driver_img_id,//当地驾照
        id_card: e.detail.value.id_card,//护照号
        img_id: e.detail.value.img_id,
        insurance_img_id: e.detail.value.insurance_img_id,
        //person_limit: e.detail.value.person_limit,//乘客数量上限
        is_supply_children_seat: that.data.checks1
      })
      wx.showToast({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”
        title: '加载中',
        icon: 'loading',
      });
      http.registerFour(param).then(data => {
        var itemData = Object.assign(new ListData(), {
          data: data
        })
        wx.hideToast();
        console.log('成功返回的数据')
        console.log(itemData)
        // // 申请成功后跳转
        wx.redirectTo({
          url: '../../../commitSuccess/commitSuccess'
        })
      })
    }

  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})